package com.zky.DAO.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionSupport;
import com.zky.DA0.ManagerDao;
import com.zky.common.HibernateSessionFactory;
import com.zky.entity.Manager;


public class ManagerDaoImp extends ActionSupport implements ManagerDao   {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public  List<Manager> list = new ArrayList<Manager>();
	
	public void delete(int id) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{
		    Manager manager = (Manager)session.get(Manager.class, id);
		    session.delete(manager);
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }
		}

		
	@SuppressWarnings("unchecked")
		public List<Manager> findAll() {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		  /* List<Manager> list = new ArrayList<Manager>();*/
		   try{
		    String hql = "from Manager";
		    Query query=session.createQuery(hql);
		    list=query.list();
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }finally{
		    session.close();
		   }
		   return list;
		}

		
		
		public Manager findById(int id) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{
			   Manager imp = (Manager)session.createQuery("from Manager s where s.id='"+id+"'").uniqueResult();
		    //Student student=(Student) session.get(Student.class, id);
		    transaction.commit();
		    return imp;
		   }catch(Exception e){
		    e.printStackTrace();
		    transaction.rollback();
		   }finally{
		    session.close();
		   }
		   return null;
		}

		
		
		public void save(Manager manager) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{
		    session.save(manager);
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }finally{
		    session.close();
		   }
		}

		
		/* (non-Javadoc)
		 * @see com.zky.DAO.impl.ManagerDao#update(com.zky.entity.Manager)
		 */
		public void update(Manager manager) {
		   Session session = HibernateSessionFactory.getSession();
		   Transaction transaction = session.beginTransaction();
		   try{
			   
		    session.update(manager);
		    transaction.commit();
		   }catch(Exception e){
		    e.printStackTrace();
		   }finally{
		    session.close();
		   }

		}


		@SuppressWarnings("unchecked")
		@Override
		public void findByLogin(String name, String psw) {
			  Session session = HibernateSessionFactory.getSession();
			  Transaction transaction = session.beginTransaction();			  
			  try{
				  String hql="from Manager as muser where muser.managername=? and muser.password=?";
				  Query query=session.createQuery(hql);
				  query.setString(0, name);
				  query.setString(1, psw);
				    List<Manager> list=query.list();
					if (null == list || list.size() == 0) {
				    	System.out.print("没有");
				    }
					else{
						System.out.print("正确");
					}
				   transaction.commit();
				   
				   }catch(Exception e){
				    e.printStackTrace();
				   }finally{
				    session.close();
			
		}


		}
		
		@SuppressWarnings("unchecked")
		public List<Manager> findAll1(String name, String psw){
			 Session session = HibernateSessionFactory.getSession();
			  Transaction transaction = session.beginTransaction();			  
			  try{
				  String hql="from Manager as muser where muser.managername=? and muser.password=?";
				  Query query=session.createQuery(hql);
				  query.setString(0, name);
				  query.setString(1, psw);
				   list=query.list();
					
				   transaction.commit();
				   
				   }catch(Exception e){
				    e.printStackTrace();
				   }finally{
				    session.close();
			
		}
			return list;


		}

		
		
		
}
		


